package com.broada.apm.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.junit.Test;
import org.yaml.snakeyaml.Yaml;

import com.broada.apm.es.bean.MetadataModel;
import com.broada.apm.model.Dimension.Area;
import com.broada.apm.utils.JsonUtil;

public class WebAreaData {
	private static final String resultFileName = "c:\\Result.txt";

	public static void main(String[] args) throws Exception {
		
		String dataArea = MetadataModel2ymal();
		
		
		String path = "D:\\svn\\apm\\branches\\dev\\module\\web\\resource\\echarts\\geoJson\\china-main-city";
		String cityMapFileName = "china-main-city-map.js";
		String cityJson = ".json";

		Map<String, String> cityMap = new HashMap<String, String>();

		File file = new File(path + "\\" + cityMapFileName);
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new FileReader(file));
			String tempString = null;
			int line = 1;
			while ((tempString = reader.readLine()) != null) {
				String[] strs = tempString.split(":");
				if (strs.length == 2) {
					cityMap.put(strs[0].trim(), strs[1].split("\"")[1]);// substring(2,
																	// 8)
				}
			}
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e1) {
				}
			}
		}

		Map<String, List<String>> quMap = new HashMap<String, List<String>>();

		Iterator iter = cityMap.entrySet().iterator();
		while (iter.hasNext()) {
			List<String> list = new ArrayList<String>();

			Map.Entry<String, String> entry = (Map.Entry<String, String>) iter
					.next();
			String key = entry.getKey();
			String value = entry.getValue();

			String filePath = path + "\\" + value + cityJson;
			String keyName = "\"name\":";
			try {
				String encoding = "UTF-8";
				File jsonFile = new File(filePath);
				if (jsonFile.isFile() && jsonFile.exists()) {
					InputStreamReader read = new InputStreamReader(
							new FileInputStream(jsonFile), encoding);
					BufferedReader bufferedReader = new BufferedReader(read);
					String lineTxt = null;
					while ((lineTxt = bufferedReader.readLine()) != null) {
						String[] keyStrs = lineTxt.split(keyName);
						for (int i = 1; i < keyStrs.length; i++) {
							String[] ns = keyStrs[i].split("\"");
							if (ns.length > 0) {
								list.add(ns[1]);//每个市的县级市区
								
								if(dataArea.indexOf(ns[1]) < 0)
									System.out.println(key + ":" + ns[1]);
							}
						}
					}
					read.close();
				} else {
					System.out.println("找不到指定的文件");
				}
			} catch (Exception e) {
				System.out.println("读取文件内容出错");
				e.printStackTrace();
			}

			quMap.put(key, list);
		}

		//清空
		File f = new File(resultFileName);
		FileWriter fw =  new FileWriter(f);
		fw.write("");
		fw.close();
		
		Iterator it = quMap.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry<String, List<String>> entry = (Map.Entry<String, List<String>>) it
					.next();
			
			String key = entry.getKey();
			List<String> list = entry.getValue();
//			System.out.println(key + ":" + list);
			
			try {
//				BufferedWriter writer = new BufferedWriter(new FileWriter(
//						new File("c:\\Result.txt")));
				FileWriter writer = new FileWriter(resultFileName, true);
				writer.write(key + ":" + list + "\n");

				writer.close();

			} catch (Exception e) {

			}
		}
	}

	
	public static String MetadataModel2ymal() throws Exception {
		InputStream input = new FileInputStream(new File(System.getProperty("user.dir") + "/../data/src/test/resources/data_createTest.yaml"));
		Yaml yaml = new Yaml();
		List<MetadataModel> m =  (List<MetadataModel>) yaml.load(input);
		return m.get(0).getEntity();
	}
	
	
	
	@Test
	public void Metadata() throws Exception {
		File file = null;
		BufferedReader br = null;
		StringBuffer buffer = null;
		String path = resultFileName;
		String retData = null;
		try {
			file = new File(path);
			buffer = new StringBuffer();
			InputStreamReader isr = new InputStreamReader(new FileInputStream(
					file), "utf-8");
			br = new BufferedReader(isr);
			int s;
			while ((s = br.read()) != -1) {
				buffer.append((char) s);
			}
			retData = buffer.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		if(retData == null)
			return ;
		
		List<Area> areas=new ArrayList<>();
		InputStream input = new FileInputStream(new File(System.getProperty("user.dir") + "/../data/src/test/resources/data_createTest.yaml"));
		Yaml yaml = new Yaml();
		List<MetadataModel> m =  (List<MetadataModel>) yaml.load(input);
		for(int i=0;i<m.get(0).getEntity().split("\n").length;i++){
			if(i!=0&&i%2!=0){
				Area area=(Area) JsonUtil.json2obj(m.get(0).getEntity().split("\n")[i], Area.class);
				areas.add(area);
				
				if(retData.indexOf(area.getName()) < 0)
					System.out.println(area.getName());
			}
		}
	}
}
